var express = require('express');
var db = require("./sql");
var app = express();
var bodyParser = require('body-parser');
var ejs = require('ejs');
var loader = require('loader-utils');
app.engine('html', ejs.__express);
app.set('views', 'mui_get_up/views');
app.set('views engine','html');

//呈现静态资源
app.use(express.static('mui_get_up/public'));

app.use(bodyParser.urlencoded({
	extended:false
}));
/*匹配所有的路由  404*/
/*app.use(function(req,res){

	res.status(404).send('这是404 表示路由没有匹配到')

})*/

//设置首页路径
app.get('/',function (req,res) {
	res.render('login')
});

app.get('/list2',function (req,res) {
	res.render('list2');
});

app.get('/list3',function (req,res) {
	res.render('list3');
});

app.get('/index',function (req,res) {
	res.render('index');
});
app.get('/RePassworld',function (req,res) {
	res.render('RePassworld');
});
app.get('/RegisterPhone',function (req,res) {
	res.render('RegisterPhone');
});
app.get('/RegisterCode',function (req,res) {
	res.render('RegisterCode');
});
app.get('/RegisterPassworld',function (req,res) {
	res.render('RegisterPassworld');
});
app.get('/search',function (req,res) {
	res.render('search');
});
app.get('/course',function (req,res) {
	res.render('course');
});
app.get('/sport',function (req,res) {
	res.render('sport');
});

app.post('/login',function(req,res,next){
	var userName = req.body.username;
	var userPwd = req.body.userpwd;
	var sql = "select * from loginuser where userName=? and password=?";
	db.query(sql,[userName,userPwd],function(err,data){
		if	(err) {
			console.log(err);
		}
		else if (data.length > 0) {
			res.render('index');
		}
		else {
			res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
			res.end("用户名或密码不正确");
		}
	})
})

app.post('/RegisterPhone',function (req,res,next) {
	var userName = req.body.tel;
	var password = req.body.pwd;
	var sql = "insert into loginuser(userName,password) values(?,?)";
	db.query(sql,[userName,password],function (err,data) {
		if (err) {
			console.log(err);
		}
		else {
			/*res.writeHead(200,{"Content-Type":"text/html;charset=utf8"});
			res.end("添加成功");*/
			res.render('RegisterCode');
		}
	})
})


app.listen(3000);
